package com.ce.pms.renovation.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.ce.pms.renovation.dao.IRenovateViolationDao;
import com.ce.pms.renovation.model.AbstractViolationDO;
import com.ce.pms.renovation.model.RenovateViolationDO;
import com.ce.pms.renovation.vo.RenovateViolationConditionVO;
import com.sitechasia.webx.core.support.Page;

/**
*
* 功能描述: 监管其违规DAO RenovateViolationDaoImpl
* <p>版权所有：中企动力
* <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
*
* @author  许心炜
* Created on：  2008-12-19
* @author  许心炜
* update on：  2008-12-19
*/

public class RenovateViolationDaoImpl extends AbstractViolationDaoImpl implements IRenovateViolationDao{

	/**
	 * 功能描述:得到指定许可证的所有违规记录<br>
	 * @param licenceId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-30<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<AbstractViolationDO> getViolationsByLicenceId(Long licenceId){
		String hql="FROM AbstractViolationDO viol where viol.renovateLicence.id=?";
		Query query=createQuery(hql, licenceId);
		return query.list();
	}

	/**
	* 获得监管期违规记录VO，包括整改记录
	* @param	renovateViolationId - 监管期违规ID
	* @return	RenovateViolationVO
	* @author  许心炜
	* @roseuid 49389F040232
	*/
	public RenovateViolationDO getRenovateViolation(Long renovateViolationId) {
		RenovateViolationDO renovateViolationDO = (RenovateViolationDO) this.findById(renovateViolationId);
		return renovateViolationDO;
	}

	/**
	 * 功能描述:  构造排序条件<br>
	 * @param queryCondition
	 * @return
	 * Create author:许心炜 <br>
	 * Create on:2008-12-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private String constructionSort(RenovateViolationConditionVO queryCondition) {
		StringBuilder result=new StringBuilder(" order by ");

		if(null==queryCondition.getOrderValue()){//点击查询时
			result.append(" ren.checkDate desc");
		}else if(queryCondition.getOrderValue().equalsIgnoreCase("ren.sysInfo.sysDate")){//点击保存按钮时
			result.append(" ren.sysInfo.sysDate desc ");
		}

		return result.toString();
	}

	/**
	 * 功能描述: 构造查询条件 <br>
	 * @param queryCondition
	 * @param paramMap
	 * @return
	 * Create author:许心炜 <br>
	 * Create on:2008-12-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private String constructionConditions(RenovateViolationConditionVO queryCondition,Map<String,Object> paramMap){
		StringBuffer hql=new StringBuffer();
		//楼栋id
		if(null != queryCondition.getBuildingId()){
			hql.append(" AND ren.renovateLicence.renovateRequisition.house.building.keyword=:buildingId ");
			paramMap.put("buildingId", queryCondition.getBuildingId());
		}

		//房号
		if(null != queryCondition.getHouseNumber()){
			hql.append(" AND ren.renovateLicence.renovateRequisition.house.houseNumber like ").append(" :houseNumber ");
			paramMap.put("houseNumber", "%" + queryCondition.getHouseNumber().trim() + "%");
		}

		//检查人
		if(null != queryCondition.getCheckMan()){
			hql.append(" AND ren.checkMan like ").append(":checkMan");
			paramMap.put("checkMan", "%" + queryCondition.getCheckMan() + "%");
		}

		//验收结果 001-通过，002-不通过
		if(null != queryCondition.getState()){
			hql.append(" AND ren.state=:state ");
			paramMap.put("state", queryCondition.getState());
		}

		//装修许可证
		if(null != queryCondition.getRenovateLicenceNO()){
			hql.append(" AND ren.renovateLicence.licenceNumber like ").append(":licenceNumber");
			paramMap.put("licenceNumber", "%" + queryCondition.getRenovateLicenceNO() + "%");
		}

		//检查开始日期
		if(null != queryCondition.getBeginCheckDate()){
			hql.append(" AND ren.checkDate>=:beginCheckDate ");
			paramMap.put("beginCheckDate", queryCondition.getBeginCheckDate());
		}

		//检查结束日期
		if(null != queryCondition.getEndCheckDate()){
			hql.append(" AND ren.checkDate< ADDDATE(:endCheckDate,1) ");
			paramMap.put("endCheckDate", queryCondition.getBeginCheckDate());
		}

		//业主
		if(null != queryCondition.getOwner()){
			hql.append(" AND ren.renovateLicence.renovateRequisition.clientNames like ").append(":owner");
			paramMap.put("owner", "%" + queryCondition.getOwner() + "%");
		}

		return hql.toString();
	}

	/**
	* 高级查询监管期违规记录列表
	* @author  许心炜
	* @param queryCondition
	* @return Page
	* @roseuid 49389F040232
	*/
	public Page queryRenovateViolations(RenovateViolationConditionVO queryCondition) {

		Map<String,Object> paramMap = new HashMap<String,Object>();
		StringBuffer strBuf = new StringBuffer("from RenovateViolationDO ren where 1=1 ");
		strBuf.append(constructionConditions(queryCondition,paramMap));
		strBuf.append(constructionSort(queryCondition));
		return super.pagedNameQueryByPageNo(strBuf.toString(),
				queryCondition.getPageNo(),
				queryCondition.getPageSize(), paramMap);
	}

	/**
	* 根据许可证ID获取不通过或者未整改的监管期违规记录
	* @param	licenceId - 许可证ID
	* @return	RenovateViolationDO
	* @author  许心炜
	* @roseuid 49389F040232
	*/
	public List<RenovateViolationDO> getRenovateViolationByLicenceID(Long licenceId) {
		String hql = "from RenovateViolationDO r where r.renovateLicence.keyword = ? and (r.state = 001 or r.state = 002)";

		Query query = this.createQuery(hql, licenceId);

		return  query.list();
	}
}
